﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YJKArchMethodLibrary.Utils.SelectMaterial;

namespace WallNaming
{
	public partial class WallNamingManagementForm : System.Windows.Forms.Form
	{
		public WallNamingManagementForm(WallNamingManager namingManager, List<WallTypeInfo> wallTypeInfoList)
		{
			this.m_NamingManager = namingManager;
			this.m_WallTypeInfoList = wallTypeInfoList;
			this.InitializeComponent();
		}

		private void NamingManagementForm_Load(object sender, EventArgs e)
		{
			this.tabControl.SelectedIndex = 0;
			this.cmbboxLayerType.Visible = false;
			this.UpdateTreeListView();
		}

		private void treeListView_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.tabControl.SelectedIndex == 1)
			{
				this.UpdateGridView();
			}
		}

		private void treeListView_ItemChecked(object sender, ItemCheckedEventArgs e)
		{
			if (this.tabControl.SelectedIndex == 0)
			{
				this.UpdateGridView();
			}
		}

		private void btnDelSelUselessType_Click(object sender, EventArgs e)
		{
			List<int> list = this.CheckedWallTypeIndex();
			if (list.Count == 0)
			{
				MessageBox.Show("当前未选择任何无用类型。", "盈建科提示");
				return;
			}
			list.Sort();
			list.Reverse();
			int num = 0;
			foreach (int index in list)
			{
				if (this.m_WallTypeInfoList[index].ElementsCount <= 0 && this.m_NamingManager.DeleteOneUselessType(this.m_WallTypeInfoList[index]))
				{
					this.m_WallTypeInfoList.RemoveAt(index);
					num++;
				}
			}
			if (num == 0 && list.Count > 0)
			{
				MessageBox.Show("个别类型不能删除，可能原因：1)该类型存在实例;2)该类型是叠层墙的组成部分;3)该类型是系统族中最后的一种墙类型。", "盈建科提示");
				return;
			}
			this.UpdateTreeListView();
		}

		private void btnDelAllUselessType_Click(object sender, EventArgs e)
		{
			int count = this.m_WallTypeInfoList.Count;
			this.m_NamingManager.DeleteAllUselessType(ref this.m_WallTypeInfoList);
			if (this.m_WallTypeInfoList.Count == count)
			{
				MessageBox.Show("个别类型不能删除，可能原因：1)该类型存在实例;2)该类型是叠层墙的组成部分;3)该类型是系统族中最后的一种墙类型。", "盈建科提示");
				return;
			}
			this.UpdateTreeListView();
		}

		private void btnUpdateList_Click(object sender, EventArgs e)
		{
			List<int> list = this.SelectedWallTypeIndex();
			if (list.Count != 1)
			{
				return;
			}
			int index = list[0];
			string text = "";
			if (!this.m_NamingManager.UpdateStructInfos(this.m_WallTypeInfoList[index], this.m_CurrentWallLayerInfo, ref text))
			{
				MessageBox.Show(text, "盈建科提示");
				this.UpdateStructInfoGridView(index);
			}
			this.cmbboxLayerType.Visible = false;
		}

		private void btnNaming_Click(object sender, EventArgs e)
		{
			List<int> list = this.CheckedWallTypeIndex();
			if (list.Count == 0)
			{
				return;
			}
			list.Sort();
			list.Reverse();
			this.dbgRegularList.Rows[0].Cells[2].Value.ToString();
			List<Wall> wallList = this.m_NamingManager.GetWallList();
			List<NamingRegularInfo> regularInfo = this.GetRegularInfo();
			int num = 0;
			foreach (NamingRegularInfo namingRegularInfo in regularInfo)
			{
				if (namingRegularInfo.m_Checked)
				{
					num++;
				}
			}
			if (num == 0)
			{
				MessageBox.Show("规则未启用", "盈建科提示");
				return;
			}
			List<WallTypeInfo> list2 = new List<WallTypeInfo>();
			foreach (int index in list)
			{
				this.m_NamingManager.CopyCreateWallType(this.m_WallTypeInfoList[index], regularInfo, wallList, false, ref list2);
				if (list2.Count > 0 && this.m_NamingManager.DeleteOneType(this.m_WallTypeInfoList[index], ref list2))
				{
					this.m_WallTypeInfoList.RemoveAt(index);
				}
			}
			this.m_WallTypeInfoList.AddRange(list2);
			this.UpdateTreeListView();
		}

		private void UpdateTreeListView()
		{
			this.treeListView.Items.Clear();
			TreeListViewItem treeListViewItem = TreeListViewEditor.AddRootItem("基本墙", 0, this.treeListView);
			TreeListViewItem treeListViewItem2 = TreeListViewEditor.AddRootItem("叠层墙", 0, this.treeListView);
			TreeListViewItem treeListViewItem3 = TreeListViewEditor.AddRootItem("幕墙", 0, this.treeListView);
			int num = 0;
			int num2 = 0;
			int num3 = 0;
			for (int i = 0; i < this.m_WallTypeInfoList.Count; i++)
			{
				WallTypeInfo wallTypeInfo = this.m_WallTypeInfoList[i];
				TreeListViewItem parent = treeListViewItem3;
				if (wallTypeInfo.TheWallKind == null)
				{
					parent = treeListViewItem;
					num += wallTypeInfo.ElementsCount;
				}
                else if ((int)wallTypeInfo.TheWallKind == 2)
				{
					parent = treeListViewItem2;
					num2 += wallTypeInfo.ElementsCount;
				}
				else
				{
					num3 += wallTypeInfo.ElementsCount;
				}
				TreeListViewItem treeListViewItem4 = TreeListViewEditor.AddItem(wallTypeInfo.FillName, 1, parent);
				TreeListViewEditor.AddColumnItem(wallTypeInfo.ElementsCount.ToString(), treeListViewItem4);
				treeListViewItem4.Tag = i;
			}
			TreeListViewEditor.AddColumnItem(num.ToString(), treeListViewItem);
			if (num == 0)
			{
				treeListViewItem.ImageIndex = 1;
			}
			TreeListViewEditor.AddColumnItem(num2.ToString(), treeListViewItem2);
			if (num2 == 0)
			{
				treeListViewItem2.ImageIndex = 1;
			}
			TreeListViewEditor.AddColumnItem(num3.ToString(), treeListViewItem3);
			if (num3 == 0)
			{
				treeListViewItem3.ImageIndex = 1;
			}
		}

		private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.UpdateGridView();
		}

		private void UpdateGridView()
		{
			if (this.tabControl.SelectedIndex == 0)
			{
				List<int> indexList = this.CheckedWallTypeIndex();
				this.UpdateNamingRegularGridView(indexList);
				return;
			}
			this.cmbboxLayerType.Visible = false;
			List<int> list = this.SelectedWallTypeIndex();
			if (list.Count != 1)
			{
				this.UpdateStructInfoGridView(-1);
				return;
			}
			int index = list[0];
			this.UpdateStructInfoGridView(index);
		}

		private void GetNamingRegularValue(List<int> indexList, ref string categoryValue, ref string levelsValue, ref string structValue, ref string structThicknessValue, ref string finishValue, ref string finishThicknessValue)
		{
			categoryValue = "AW";
			if (this.checkBoxStruct.Checked)
			{
				categoryValue = "SW";
			}
			List<Wall> wallList = this.m_NamingManager.GetWallList();
			if (this.m_WallTypeInfoList[indexList[0]].TheWallKind != null)
			{
				return;
			}
			levelsValue = this.GetLevelsInfoString(this.m_NamingManager.GetLevelsInfo(this.m_WallTypeInfoList[indexList[0]].WallTypeId, wallList));
			CompoundStructureLayer wallStructLayerInfo = this.m_NamingManager.GetWallStructLayerInfo(this.m_WallTypeInfoList[indexList[0]].ComStructure);
			structValue = this.m_NamingManager.GetMaterialName(wallStructLayerInfo);
			structThicknessValue = "0.0";
			if (wallStructLayerInfo != null)
			{
				structThicknessValue = this.FeetToMM(wallStructLayerInfo.Width).ToString();
			}
			this.m_NamingManager.GetWallFinishLayerInfo(this.m_WallTypeInfoList[indexList[0]].ComStructure, ref finishValue, ref finishThicknessValue);
			for (int i = 1; i < indexList.Count; i++)
			{
				if (this.m_WallTypeInfoList[indexList[i]].TheWallKind != null)
				{
					return;
				}
				CompoundStructureLayer wallStructLayerInfo2 = this.m_NamingManager.GetWallStructLayerInfo(this.m_WallTypeInfoList[indexList[i]].ComStructure);
				string materialName = this.m_NamingManager.GetMaterialName(wallStructLayerInfo2);
				if (structValue.CompareTo(materialName) != 0)
				{
					structValue = "多种";
				}
				string strB = "0.0";
				if (wallStructLayerInfo2 != null)
				{
					strB = this.FeetToMM(wallStructLayerInfo2.Width).ToString();
				}
				if (structThicknessValue.CompareTo(strB) != 0)
				{
					structThicknessValue = "多种";
				}
				string strB2 = "";
				string strB3 = "";
				this.m_NamingManager.GetWallFinishLayerInfo(this.m_WallTypeInfoList[indexList[i]].ComStructure, ref strB2, ref strB3);
				if (finishValue.CompareTo(strB2) != 0)
				{
					finishValue = "多种";
				}
				if (finishThicknessValue.CompareTo(strB3) != 0)
				{
					finishThicknessValue = "多种";
				}
				string levelsInfoString = this.GetLevelsInfoString(this.m_NamingManager.GetLevelsInfo(this.m_WallTypeInfoList[indexList[i]].WallTypeId, wallList));
				if (levelsValue.CompareTo(levelsInfoString) != 0)
				{
					levelsValue = "多种";
				}
			}
		}

		private string GetLevelsInfoString(List<Level> levelList)
		{
			string text = "";
			int num = 0;
			foreach (Level level in levelList)
			{
				if (num == 0)
				{
					text = level.Name;
				}
				else
				{
					text = text + "_" + level.Name;
				}
				num++;
			}
			return text;
		}

		private double FeetToMM(double val)
		{
			return val * 304.8;
		}

		private void UpdateNamingRegularGridView(List<int> indexList)
		{
			this.dbgRegularList.Rows.Clear();
			if (indexList.Count == 0)
			{
				return;
			}
			string regularValue = "";
			string text = "";
			string regularValue2 = "";
			string regularValue3 = "";
			string text2 = "";
			string text3 = "";
			this.GetNamingRegularValue(indexList, ref regularValue, ref text, ref regularValue2, ref regularValue3, ref text2, ref text3);
			List<NamingRegularInfo> list = new List<NamingRegularInfo>();
			string regularName = NamingRegularInfo.GetRegularName(NamingRegularType.NRT_Categoty);
			NamingRegularInfo item = new NamingRegularInfo(NamingRegularType.NRT_Categoty, regularName, regularValue, true, true, true, true);
			list.Add(item);
			string regularName2 = NamingRegularInfo.GetRegularName(NamingRegularType.NRT_Region);
			NamingRegularInfo item2 = new NamingRegularInfo(NamingRegularType.NRT_Region, regularName2, "分区1", true, false, false, true);
			list.Add(item2);
			string regularName3 = NamingRegularInfo.GetRegularName(NamingRegularType.NRT_Level);
			bool flag = true;
			if (text.CompareTo("") == 0)
			{
				flag = false;
			}
			NamingRegularInfo item3 = new NamingRegularInfo(NamingRegularType.NRT_Level, regularName3, text, flag, true, true, true);
			list.Add(item3);
			string regularName4 = NamingRegularInfo.GetRegularName(NamingRegularType.NRT_Struct);
			NamingRegularInfo item4 = new NamingRegularInfo(NamingRegularType.NRT_Struct, regularName4, regularValue2, true, true, true, true);
			list.Add(item4);
			string regularName5 = NamingRegularInfo.GetRegularName(NamingRegularType.NRT_StructThickness);
			NamingRegularInfo item5 = new NamingRegularInfo(NamingRegularType.NRT_StructThickness, regularName5, regularValue3, true, true, true, true);
			list.Add(item5);
			flag = true;
			if (text2.CompareTo("") == 0)
			{
				flag = false;
			}
			string regularName6 = NamingRegularInfo.GetRegularName(NamingRegularType.NRT_Finish);
			NamingRegularInfo item6 = new NamingRegularInfo(NamingRegularType.NRT_Finish, regularName6, text2, flag, false, flag, false);
			list.Add(item6);
			flag = true;
			if (text3.CompareTo("") == 0)
			{
				flag = false;
			}
			string regularName7 = NamingRegularInfo.GetRegularName(NamingRegularType.NRT_FinishThickness);
			NamingRegularInfo item7 = new NamingRegularInfo(NamingRegularType.NRT_FinishThickness, regularName7, text3, flag, false, flag, false);
			list.Add(item7);
			string regularName8 = NamingRegularInfo.GetRegularName(NamingRegularType.NRT_Suffix);
			NamingRegularInfo item8 = new NamingRegularInfo(NamingRegularType.NRT_Suffix, regularName8, "", false, false, false, true);
			list.Add(item8);
			foreach (NamingRegularInfo namingRegularInfo in list)
			{
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				DataGridViewCheckBoxCell dataGridViewCheckBoxCell = new DataGridViewCheckBoxCell();
				dataGridViewCheckBoxCell.Value = namingRegularInfo.m_Checked;
				dataGridViewRow.Cells.Add(dataGridViewCheckBoxCell);
				DataGridViewTextBoxCell dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell.Value = namingRegularInfo.m_RegularName;
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell);
                dataGridViewRow.Cells[1].Style.BackColor = System.Drawing.Color.Gainsboro;
				DataGridViewTextBoxCell dataGridViewTextBoxCell2 = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell2.Value = namingRegularInfo.m_RegularValue;
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell2);
				dataGridViewRow.Cells[2].ReadOnly = namingRegularInfo.m_ValueReadOnly;
				if (namingRegularInfo.m_ValueReadOnly)
				{
                    dataGridViewRow.Cells[2].Style.BackColor = System.Drawing.Color.Gainsboro;
				}
				DataGridViewCheckBoxCell dataGridViewCheckBoxCell2 = new DataGridViewCheckBoxCell();
				dataGridViewCheckBoxCell2.Value = namingRegularInfo.m_AutoName;
				dataGridViewRow.Cells.Add(dataGridViewCheckBoxCell2);
				dataGridViewRow.Cells[3].ReadOnly = namingRegularInfo.m_AutoNameReadOnly;
				if (namingRegularInfo.m_AutoNameReadOnly)
				{
                    dataGridViewRow.Cells[3].Style.BackColor = System.Drawing.Color.Gainsboro;
				}
				this.dbgRegularList.Rows.Add(dataGridViewRow);
			}
		}

		private void UpdateStructInfoGridView(int index)
		{
			if (index == -1)
			{
				this.dbgLayerInfoList.Enabled = false;
				this.btnUpdateList.Enabled = false;
				this.dbgLayerInfoList.Rows.Clear();
				return;
			}
			if (this.m_WallTypeInfoList[index].TheWallKind == null)
			{
				this.dbgLayerInfoList.Enabled = true;
				this.btnUpdateList.Enabled = true;
				this.m_CurrentWallLayerInfo.Clear();
				this.m_CurrentWallLayerInfo = this.m_NamingManager.GetWallLayerInfoListByType(this.m_WallTypeInfoList[index].ComStructure);
				this.UpdateCurrentLayerStructInfoGridView(0);
				return;
			}
			this.dbgLayerInfoList.Enabled = false;
			this.btnUpdateList.Enabled = false;
			this.dbgLayerInfoList.Rows.Clear();
		}

		private void UpdateCurrentLayerStructInfoGridView(int seletedRowIndex)
		{
			this.dbgLayerInfoList.Rows.Clear();
			foreach (StructLayersInfo structLayersInfo in this.m_CurrentWallLayerInfo)
			{
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				DataGridViewTextBoxCell dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell.Value = structLayersInfo.m_Name;
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell);
				DataGridViewButtonCell dataGridViewButtonCell;
				if (structLayersInfo.m_StructureLayer == null)
				{
					dataGridViewButtonCell = new DataGridViewDisableButtonCell();
					((DataGridViewDisableButtonCell)dataGridViewButtonCell).Enabled = false;
				}
				else
				{
					dataGridViewButtonCell = new DataGridViewButtonCell();
				}
				dataGridViewButtonCell.Value = structLayersInfo.m_MaterialName;
				dataGridViewRow.Cells.Add(dataGridViewButtonCell);
				DataGridViewTextBoxCell dataGridViewTextBoxCell2 = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell2.Value = structLayersInfo.m_Thickness;
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell2);
				if (structLayersInfo.m_StructureLayer == null)
				{
					dataGridViewRow.ReadOnly = true;
                    dataGridViewRow.DefaultCellStyle.BackColor = System.Drawing.Color.LightGray;
				}
				this.dbgLayerInfoList.Rows.Add(dataGridViewRow);
			}
			if (seletedRowIndex > -1 && seletedRowIndex < this.dbgLayerInfoList.RowCount)
			{
				this.dbgLayerInfoList.Rows[seletedRowIndex].Cells[0].Selected = true;
			}
		}

		private List<int> CheckedWallTypeIndex()
		{
			List<int> list = new List<int>();
			if (this.treeListView.CheckedItems.Count<TreeListViewItem>() == 0)
			{
				return list;
			}
			foreach (TreeListViewItem treeListViewItem in this.treeListView.CheckedItems)
			{
				if (treeListViewItem.Level != 0)
				{
					int item = Convert.ToInt32(treeListViewItem.Tag.ToString());
					list.Add(item);
				}
			}
			return list;
		}

		private List<int> SelectedWallTypeIndex()
		{
			List<int> list = new List<int>();
			if (this.treeListView.SelectedItems.Count == 0)
			{
				return list;
			}
			foreach (object obj in this.treeListView.SelectedItems)
			{
				TreeListViewItem treeListViewItem = (TreeListViewItem)obj;
				int index;
				if (treeListViewItem.Level == 0)
				{
					IEnumerator enumerator2 = treeListViewItem.Items.GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							object obj2 = enumerator2.Current;
							TreeListViewItem treeListViewItem2 = (TreeListViewItem)obj2;
							int index2 = Convert.ToInt32(treeListViewItem2.Tag.ToString());
							int num = list.FindIndex((int value) => value == index2);
							if (num == -1)
							{
								list.Add(index2);
							}
						}
						continue;
					}
				}
				index = Convert.ToInt32(treeListViewItem.Tag.ToString());
				int num2 = list.FindIndex((int value) => value == index);
				if (num2 == -1)
				{
					list.Add(index);
				}
			}
			return list;
		}

		private void dbgLayerInfoList_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			this.ClickMaterialColumn(sender, e);
			this.ClickLayerColumn(sender, e);
		}

		private void ClickMaterialColumn(object sender, DataGridViewCellEventArgs e)
		{
			if (e.ColumnIndex != 1 || e.RowIndex == -1)
			{
				return;
			}
			string text = this.dbgLayerInfoList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
			if (text.IndexOf("包络") != -1)
			{
				return;
			}
			Document document = this.m_NamingManager.CmdData.Application.ActiveUIDocument.Document;
			Transaction transaction = new Transaction(document);
			transaction.Start("change info");
			try
			{
				string text2 = "";
				if (!MaterialSelectManager.MaterialSelect(document, text, ref text2))
				{
					if (transaction != null && transaction.HasStarted() && !transaction.HasEnded())
					{
						transaction.RollBack();
					}
				}
				else
				{
					this.dbgLayerInfoList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = text2;
					this.m_CurrentWallLayerInfo[e.RowIndex].m_MaterialName = text2;
					transaction.Commit();
				}
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				if (transaction != null && transaction.HasStarted() && !transaction.HasEnded())
				{
					transaction.RollBack();
				}
			}
		}

		private void ClickLayerColumn(object sender, DataGridViewCellEventArgs e)
		{
			if (e.ColumnIndex != 0 || e.RowIndex == -1)
			{
				return;
			}
			DataGridViewCell dataGridViewCell = this.dbgLayerInfoList.Rows[e.RowIndex].Cells[e.ColumnIndex];
			string text = dataGridViewCell.Value.ToString();
			if (text.IndexOf("核心边界") != -1)
			{
				return;
			}
			int x = this.dbgLayerInfoList.Location.X;
			int y = this.dbgLayerInfoList.Location.Y;
			Rectangle cellDisplayRectangle = this.dbgLayerInfoList.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false);
			int x2 = cellDisplayRectangle.X;
			int y2 = cellDisplayRectangle.Y;
			int x3 = x + x2;
			int y3 = y + y2;
			this.cmbboxLayerType.Location = new System.Drawing.Point(x3, y3);
			this.cmbboxLayerType.Size = cellDisplayRectangle.Size;
			int selectedIndex = this.cmbboxLayerType.FindString(dataGridViewCell.Value.ToString());
			this.cmbboxLayerType.SelectedIndex = selectedIndex;
			this.cmbboxLayerType.Visible = true;
			this.cmbboxLayerType.BringToFront();
		}

		private void cmbboxLayerType_SelectedIndexChanged(object sender, EventArgs e)
		{
			int rowIndex = this.dbgLayerInfoList.CurrentCell.RowIndex;
			StructLayersInfo structLayersInfo = this.m_CurrentWallLayerInfo[rowIndex];
			if (structLayersInfo.m_StructureLayer == null)
			{
				this.cmbboxLayerType.Visible = false;
				return;
			}
			string text = this.cmbboxLayerType.Items[this.cmbboxLayerType.SelectedIndex].ToString();
			if (text.CompareTo(structLayersInfo.m_Name) == 0)
			{
				this.cmbboxLayerType.Visible = false;
				return;
			}
			this.cmbboxLayerType.Visible = false;
			this.m_CurrentWallLayerInfo[rowIndex].m_Name = text;
            MaterialFunctionAssignment function = (MaterialFunctionAssignment)1;
			string a;
			if ((a = text) != null)
			{
				if (!(a == "衬底"))
				{
					if (!(a == "保温层/空气层"))
					{
						if (!(a == "面层1"))
						{
							if (!(a == "面层2"))
							{
								if (a == "涂膜层")
								{
                                    function = (MaterialFunctionAssignment)100;
								}
							}
							else
							{
                                function = (MaterialFunctionAssignment)5;
							}
						}
						else
						{
                            function = (MaterialFunctionAssignment)4;
						}
					}
					else
					{
                        function = (MaterialFunctionAssignment)3;
					}
				}
				else
				{
                    function = (MaterialFunctionAssignment)2;
				}
			}
			CompoundStructureLayer compoundStructureLayer = new CompoundStructureLayer(this.m_CurrentWallLayerInfo[rowIndex].m_StructureLayer);
			compoundStructureLayer.Function = function;
			this.m_CurrentWallLayerInfo[rowIndex].m_StructureLayer = compoundStructureLayer;
			this.UpdateCurrentLayerStructInfoGridView(rowIndex);
		}

		private void dbgLayerInfoList_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			TextBox textBox = e.Control as TextBox;
			if (textBox != null)
			{
				textBox.TextChanged += this.DGVTextBoxTextChanged;
			}
		}

		private void DGVTextBoxTextChanged(object sender, EventArgs e)
		{
			List<int> list = this.SelectedWallTypeIndex();
			if (list.Count != 1)
			{
				return;
			}
			int num = 2;
			if (this.dbgLayerInfoList.CurrentCell.ColumnIndex == num)
			{
				int rowIndex = this.dbgLayerInfoList.CurrentCell.RowIndex;
				string text = ((TextBox)sender).Text;
				string thickness = this.m_CurrentWallLayerInfo[rowIndex].m_Thickness;
				int num2 = 0;
				try
				{
					num2 = Convert.ToInt32(text);
				}
				catch (Exception)
				{
					((TextBox)sender).Text = thickness;
					return;
				}
				string text2 = this.dbgLayerInfoList.Rows[rowIndex].Cells[0].Value.ToString();
				if (text2.CompareTo("涂膜层") == 0)
				{
					if (num2 != 0)
					{
						MessageBox.Show(text2 + "层厚度只能等于零", "盈建科提示");
						((TextBox)sender).Text = thickness;
						return;
					}
				}
				else if (num2 <= 0)
				{
					MessageBox.Show(text2 + "层厚度不能小于等于零", "盈建科提示");
					((TextBox)sender).Text = thickness;
					return;
				}
				this.m_CurrentWallLayerInfo[rowIndex].m_Thickness = num2.ToString();
				return;
			}
		}

		private void checkBoxStruct_CheckedChanged(object sender, EventArgs e)
		{
			if (this.dbgRegularList.Rows.Count == 0)
			{
				return;
			}
			if (this.checkBoxStruct.Checked)
			{
				this.dbgRegularList.Rows[0].Cells[2].Value = "SW";
				return;
			}
			this.dbgRegularList.Rows[0].Cells[2].Value = "AW";
		}

		private void btnClone_Click(object sender, EventArgs e)
		{
			List<int> list = this.CheckedWallTypeIndex();
			if (list.Count == 0)
			{
				return;
			}
			list.Sort();
			list.Reverse();
			this.dbgRegularList.Rows[0].Cells[2].Value.ToString();
			List<Wall> wallInstanceList = new List<Wall>();
			List<NamingRegularInfo> regularInfo = this.GetRegularInfo();
			int num = 0;
			foreach (NamingRegularInfo namingRegularInfo in regularInfo)
			{
				if (namingRegularInfo.m_Checked)
				{
					num++;
				}
			}
			if (num == 0)
			{
				MessageBox.Show("规则未启用", "盈建科提示");
				return;
			}
			List<WallTypeInfo> collection = new List<WallTypeInfo>();
			foreach (int index in list)
			{
				this.m_NamingManager.CopyCreateWallType(this.m_WallTypeInfoList[index], regularInfo, wallInstanceList, true, ref collection);
			}
			this.m_WallTypeInfoList.AddRange(collection);
			this.UpdateTreeListView();
		}

		private List<NamingRegularInfo> GetRegularInfo()
		{
			List<NamingRegularInfo> list = new List<NamingRegularInfo>();
			for (int i = 0; i < this.dbgRegularList.RowCount; i++)
			{
				DataGridViewRow dataGridViewRow = this.dbgRegularList.Rows[i];
				bool flag = Convert.ToBoolean(dataGridViewRow.Cells[0].Value.ToString());
				if (flag)
				{
					NamingRegularType type = (NamingRegularType)i;
					bool isChecked = Convert.ToBoolean(dataGridViewRow.Cells[0].Value.ToString());
					string regularName = dataGridViewRow.Cells[1].Value.ToString();
					string regularValue = "";
					if (dataGridViewRow.Cells[2].Value != null)
					{
						regularValue = dataGridViewRow.Cells[2].Value.ToString();
					}
					bool autoName = Convert.ToBoolean(dataGridViewRow.Cells[3].Value.ToString());
					bool readOnly = dataGridViewRow.Cells[3].ReadOnly;
					NamingRegularInfo item = new NamingRegularInfo(type, regularName, regularValue, isChecked, true, autoName, readOnly);
					list.Add(item);
				}
			}
			return list;
		}

		private void btnSuffixSetting_Click(object sender, EventArgs e)
		{
			SuffixMarkSettingForm suffixMarkSettingForm = new SuffixMarkSettingForm();
			suffixMarkSettingForm.ShowDialog();
		}

		private void btnMoveUp_Click(object sender, EventArgs e)
		{
			if (this.dbgLayerInfoList.SelectedCells.Count != 1)
			{
				return;
			}
			int rowIndex = this.dbgLayerInfoList.SelectedCells[0].RowIndex;
			if (rowIndex == 0)
			{
				return;
			}
			if (!this.IsValiditySwap(this.m_CurrentWallLayerInfo[rowIndex - 1], this.m_CurrentWallLayerInfo[rowIndex]))
			{
				return;
			}
			this.m_CurrentWallLayerInfo.Insert(rowIndex - 1, this.m_CurrentWallLayerInfo[rowIndex]);
			this.m_CurrentWallLayerInfo.RemoveAt(rowIndex + 1);
			this.UpdateCurrentLayerStructInfoGridView(rowIndex - 1);
			this.cmbboxLayerType.Visible = false;
		}

		private void btnMoveDown_Click(object sender, EventArgs e)
		{
			if (this.dbgLayerInfoList.SelectedCells.Count != 1)
			{
				return;
			}
			int rowIndex = this.dbgLayerInfoList.SelectedCells[0].RowIndex;
			if (rowIndex == this.dbgLayerInfoList.RowCount - 1)
			{
				return;
			}
			if (!this.IsValiditySwap(this.m_CurrentWallLayerInfo[rowIndex], this.m_CurrentWallLayerInfo[rowIndex + 1]))
			{
				return;
			}
			this.m_CurrentWallLayerInfo.Insert(rowIndex + 2, this.m_CurrentWallLayerInfo[rowIndex]);
			this.m_CurrentWallLayerInfo.RemoveAt(rowIndex);
			this.UpdateCurrentLayerStructInfoGridView(rowIndex + 1);
			this.cmbboxLayerType.Visible = false;
		}

		private void btnAddLayer_Click(object sender, EventArgs e)
		{
			if (this.dbgLayerInfoList.SelectedCells.Count != 1)
			{
				return;
			}
			int rowIndex = this.dbgLayerInfoList.SelectedCells[0].RowIndex;
			StructLayersInfo structLayersInfo = this.m_CurrentWallLayerInfo[rowIndex];
			if (structLayersInfo.m_StructureLayer == null)
			{
				MessageBox.Show("不能通过核心边界创建新层", "盈建科提示");
				return;
			}
			CompoundStructureLayer structureLayer = new CompoundStructureLayer(structLayersInfo.m_StructureLayer);
			StructLayersInfo item = new StructLayersInfo(structureLayer, structLayersInfo.m_Name, structLayersInfo.m_LayerId, structLayersInfo.m_MaterialName, structLayersInfo.m_Thickness);
			if (rowIndex == this.dbgLayerInfoList.RowCount - 1)
			{
				this.m_CurrentWallLayerInfo.Add(item);
			}
			else
			{
				this.m_CurrentWallLayerInfo.Insert(rowIndex + 1, item);
			}
			this.UpdateCurrentLayerStructInfoGridView(rowIndex);
			this.cmbboxLayerType.Visible = false;
		}

		private void btnDeleteLayer_Click(object sender, EventArgs e)
		{
			if (this.dbgLayerInfoList.SelectedCells.Count != 1)
			{
				return;
			}
			int rowIndex = this.dbgLayerInfoList.SelectedCells[0].RowIndex;
			StructLayersInfo structLayersInfo = this.m_CurrentWallLayerInfo[rowIndex];
			if (structLayersInfo.m_StructureLayer == null)
			{
				MessageBox.Show("核心边界不能删除", "盈建科提示");
				return;
			}
            if ((int)structLayersInfo.m_StructureLayer.Function == 1)
			{
				int num = 0;
				foreach (StructLayersInfo structLayersInfo2 in this.m_CurrentWallLayerInfo)
				{
                    if (structLayersInfo2.m_StructureLayer != null && (int)structLayersInfo2.m_StructureLayer.Function == 1)
					{
						num++;
					}
				}
				if (num == 1)
				{
					MessageBox.Show("必须保留一个结构层", "盈建科提示");
					return;
				}
			}
			this.m_CurrentWallLayerInfo.RemoveAt(rowIndex);
			this.UpdateCurrentLayerStructInfoGridView(rowIndex);
			this.cmbboxLayerType.Visible = false;
		}

		private bool IsValiditySwap(StructLayersInfo exteriorLayer, StructLayersInfo interiorLayer)
		{
			return true;
		}

		private void dbgRegularList_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			int columnIndex = this.dbgRegularList.CurrentCell.ColumnIndex;
			if (columnIndex == 2)
			{
				TextBox textBox = e.Control as TextBox;
				if (textBox != null)
				{
					textBox.ImeMode = ImeMode.Hangul;
				}
			}
		}

		private WallNamingManager m_NamingManager;

		private List<WallTypeInfo> m_WallTypeInfoList;

		private List<StructLayersInfo> m_CurrentWallLayerInfo = new List<StructLayersInfo>();
	}
}
